package com.sia.springcloud.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sia.springcloud.model.UserInfo;
import com.sia.springcloud.result.ResultUtil;
import com.sia.springcloud.service.UserInfoService;
import com.sia.springcloud.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author Sirius
 * @since 2022-03-07
 * <p>
 * 登录相关方法：登录、退出登录、注册、注销账号、修改用户信息
 */

@Slf4j
@RestController
@RequestMapping("//registerController")
public class RegisterController {

    @Resource
    private UserInfoService userInfoService;

    /**
     * 用户登录
     * <p>
     * 用户登录成功则发放token
     * <p>
     * 这里应该返回前端用户信息：是否为管理员！
     *
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return ResultUtil.createSimpleFailureJson("请填写完整的用户名与密码");
        }
        return userInfoService.login(username, password);

        // 需要判断是否为管理员用户
    }


    /**
     * 查询用户信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/queryUserInfo")
    public String queryUserInfo(HttpServletRequest request) {
        String username = request.getParameter("username");

        return userInfoService.queryUserInfo(username);
    }


    /**
     * 退出登录
     * <p>
     * redis中删除，同时
     *
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        String token = request.getParameter("token");
        System.out.println("token:"+token);

        //String token = request.getHeader("token");

        if (!StringUtils.isEmpty(token)) {
            Claims claims = JwtUtils.parseJWT(token);
            String username = claims.getSubject();
            return userInfoService.logout(username);
        }
        return ResultUtil.createSimpleFailureJson("获取token失败");
    }


    /**
     * 用户注册
     * <p>
     * 只能注册普通用户
     *
     * @param request
     * @return
     */
    @RequestMapping("/register")
    public String register(HttpServletRequest request) {
        try {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            String company = request.getParameter("company");
            String telephone = request.getParameter("telephone");
            UserInfo userInfo = new UserInfo();

            if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
                return ResultUtil.createSimpleFailureJson("用户名和密码不能为空");
            }
            userInfo.setUsername(username);
            userInfo.setPassword(password);

            userInfo.setRole("commonUser");
            userInfo.setPermission(1);

            if (!StringUtils.isEmpty(email)) {
                userInfo.setEmail(email);
            }
            if (!StringUtils.isEmpty(company)) {
                userInfo.setCompany(company);
            }
            if (!StringUtils.isEmpty(telephone)) {
                userInfo.setTelephone(telephone);
            }
            return userInfoService.insertUser(userInfo);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResultUtil.createSimpleFailureJson("用户注册失败");
    }


    /**
     * 注销账号
     * <p>
     * 首先你要登录才能注销账号
     *
     * @param request
     * @return
     */
    @RequestMapping("/cancelAccount")
    public String cancelAccount(HttpServletRequest request) {
        String token = request.getParameter("token");
        System.out.println("token:"+token);
        //String token = request.getHeader("token");

        if (!StringUtils.isEmpty(token)) {
            Claims claims = JwtUtils.parseJWT(token);
            String username = claims.getSubject();
            return userInfoService.cancelAccount(username);
        }
        return ResultUtil.createSimpleFailureJson("获取token失败");
    }


    /**
     * 修改用户信息
     * <p>
     * 修改用户信息之后应该重新登录
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateUser")
    public String updateUserInfo(HttpServletRequest request) {
        log.info("更新用户信息");
        try {
            String id = request.getParameter("id");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            String company = request.getParameter("company");
            String telephone = request.getParameter("telephone");
            UserInfo userInfo = new UserInfo();
            log.info("输入的用户信息为："+userInfo.toString());

            if (StringUtils.isEmpty(id) || StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
                return ResultUtil.createSimpleFailureJson("用户名和密码不能为空");
            }

            userInfo.setId(Integer.parseInt(id));
            userInfo.setUsername(username);
            userInfo.setPassword(password);
            userInfo.setRole("commonUser");
            userInfo.setPermission(1);
            if (StringUtils.isNotEmpty(email)) {
                userInfo.setEmail(email);
            } else {
                userInfo.setEmail("");
            }
            if (StringUtils.isNotEmpty(company)) {
                userInfo.setCompany(company);
            } else {
                userInfo.setCompany("");
            }
            if (StringUtils.isNotEmpty(telephone)) {
                userInfo.setTelephone(telephone);
            } else {
                userInfo.setTelephone("");
            }
            return userInfoService.updateUser(userInfo);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResultUtil.createSimpleFailureJson("用户修改失败");
    }
}
